package org.fuys.coder.application.admin;

import org.fuys.coder.domain.config.model.AppConfigSupport;
import org.fuys.coder.domain.config.model.req.AppConfigAddVersionReq;
import org.fuys.coder.domain.config.model.req.AppConfigCommonReq;
import org.fuys.coder.domain.config.model.req.AppConfigUpdateReq;
import org.fuys.coder.domain.config.service.BaseConfig;
import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.entity.Result;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.auth.model.annotation.PermissionCertify;
import org.fuys.coder.domain.auth.model.vo.permission.PermissionTypeVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application.admin
 * @className: ConfigurationAdminController
 * @author: WangJing
 * @description: 配置管理控制器 这里要注意的是 如果有一些配置需要经常更改 配置管理需要存在数据库中
 * @date: 2024/8/17 16:30
 * @version: 1.0
 */

@RestController
@RequestMapping("/fuyscoder/admin/config")
public class ConfigurationAdminController {

    @Resource
    private AppConfigSupport appConfigSupport;

    @PostMapping("/type/list")
    @PermissionCertify(permission = PermissionTypeVO.CONFIG_SELECT)
    public Result getConfigList(@RequestBody AppConfigCommonReq req){
        try {
            final List<? extends BaseConfig> typeConfigList = appConfigSupport.getTypeConfigList(req.getType());
            return Result.ok().data(typeConfigList);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultMessageConstants.SYSTEM_ERROR);
        }
    }

    @PostMapping("/switch/version")
    @PermissionCertify(permission = PermissionTypeVO.CONFIG_MODIFY)
    public Result switchConfigVersion(@RequestBody AppConfigCommonReq req){
        try {
            appConfigSupport.switchConfigVersion(req.getType(),req.getVersion());
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultMessageConstants.SYSTEM_ERROR);
        }
        return Result.ok();
    }

    @PostMapping("/add/version")
    @PermissionCertify(permission = PermissionTypeVO.CONFIG_MODIFY)
    public Result addConfigVersion(@RequestParam("type")Integer type,
                                   @RequestBody AppConfigAddVersionReq req){
        try {
            appConfigSupport.addConfigVersion(type,req);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultMessageConstants.SYSTEM_ERROR);
        }
        return Result.ok();
    }

    @PostMapping("/update/version")
    @PermissionCertify(permission = PermissionTypeVO.CONFIG_MODIFY)
    public Result updateVersionConfigValue(@RequestBody AppConfigUpdateReq req){
        try {
            appConfigSupport.updateConfigValue(req.getType(),req.getKey(),req.getValue(),req.getVersion());
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        } catch (Exception e){
            return Result.error(ResultMessageConstants.SYSTEM_ERROR);
        }
        return Result.ok();
    }

}
